#ifndef Goomba_h
#define Goomba_h

#include "MovableObject.h"

#define GOOMBA_WIDTH 45
#define GOOMBA_HEIGHT 53
#define GOOMBA_ANIMATE_RATE 5
#define GOOMBA_SPEED 0.2f
#define GOOMBA_TIMEDISAPPEAR 1

class Goomba : public MovableObject 
{
public:
	Goomba(int id, D3DXVECTOR2 position, int MovingWidth): MovableObject(id, position, MovingWidth)
	{
		_w = GOOMBA_WIDTH;
		_h = GOOMBA_HEIGHT;
		_speedX = GOOMBA_SPEED;
		_speedY = 2.5f;
		_type = GoombaType;
		_movingWidth = MovingWidth;
		leftStop = position.x - MovingWidth/2;
		if(leftStop < 0) leftStop = 0;
		rightStop = position.x + MovingWidth/2;
		_vx = _speedX;
	}
	~Goomba(){}

	void Update(int Delta, vector<GameObject*> vectorObj, QRectangle bound)
	{
		if(_state != disappear)
		{
			if(_countTimeDisappear >= GOOMBA_TIMEDISAPPEAR)
				_state = disappear;
			MovableObject::Update(Delta, vectorObj, bound);
			if(_state == hurt)
			{
				_vx = 0;
				_vy = 0;
				_countTimeDisappear++;
			}

			if((_position.x < leftStop + GOOMBA_WIDTH/2) || (_position.x > rightStop - GOOMBA_WIDTH/2)
				|| (_position.x <= GOOMBA_WIDTH/2) || (_position.x > ViewPort::MapWidth() - GOOMBA_WIDTH/2))
			{
				_vx = -_vx;
			}

			DWORD now = GetTickCount();
			if (now - last_time > 1000 / GOOMBA_ANIMATE_RATE)
			{
				if (_vx != 0) _moveSprite -> Next();
				last_time = now;
			}
		}
	}

	void LoadResources(LPD3DXSPRITE SpriteHandler, Texture *texture)
	{
		_moveSprite = new AnimatedSprite(SpriteHandler, texture, GOOMBA_WIDTH, GOOMBA_HEIGHT, 2, 3, 0);
		_dieSprite = new Sprite(SpriteHandler, texture, GOOMBA_WIDTH, GOOMBA_HEIGHT, 3, 2);
	}
	void RenderFrame(LPDIRECT3DSURFACE9 BackBuffer, ViewPort *viewport)
	{
		if(_state!=disappear)
		{
			if(_state == hurt && _vx >= 0)
			{
				_dieSprite -> Render(BackBuffer, (int)_position.x, (int)_position.y, 255, 1, 1, viewport);
			}
			else if(_state == hurt && _vx < 0)
			{
				_dieSprite -> Render(BackBuffer, (int)_position.x, (int)_position.y, 255, -1, 1, viewport);
			}
			else if (_vx >= 0)
			{
				_moveSprite -> Render(BackBuffer, (int)_position.x, (int)_position.y, 255, 1, 1, viewport);
			}
			else if (_vx < 0)
				_moveSprite -> Render(BackBuffer, (int)_position.x, (int)_position.y, 255, -1, 1, viewport);
		}
	}

private:
	Sprite *_dieSprite;
};
#endif